package main;

import heartBeat.MasterHeartBeatApiInterface;
import heartBeat.MasterHeartBeatApi;

import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;

import tool.GetProper;
import tool.MysqlApi;

import dataStore.SlaverDataStoreApi;
import dataStore.SlaverDataStoreApiInterface;
import dataStore.UserDataStoreApi;
import dataStore.UserDataStoreApiInterface;
import dataStruct.DataStoreTask;
import dataStruct.DataStoreTaskBag;
import dataStruct.FtpFileDowndedCount;
import dataStruct.SlaverStatus;

public final class CreateRegistry {
	public static void createRegistry(Map<Integer,	 SlaverStatus>slaverMap, 
			Queue<DataStoreTask> dataStoreTaskChunkQueue, 
			Queue<String> dataStoreTaskSlaverChunkQueue,
			Map<String, DataStoreTaskBag> dataStoreTaskSlaverChunkMap, 
			Map<String, FtpFileDowndedCount> ftpFileDownedSizeMap) 
			throws AlreadyBoundException, RemoteException, InterruptedException, SQLException, NotBoundException {
		
		Properties masterProper = GetProper.get_properties("conf/config.ini");
		 new MysqlApi(masterProper);
//		int port = 2001;
		Registry registryHB = 	LocateRegistry.createRegistry(Short.parseShort(masterProper.getProperty("rmi_masterHeartBeatPort")));
		/**********HeartBeat registry**********/
		MasterHeartBeatApi	 masterHBApi = new MasterHeartBeatApi(
				slaverMap, 
				dataStoreTaskSlaverChunkQueue,
				dataStoreTaskSlaverChunkMap
				);
		MasterHeartBeatApiInterface masterHeartBeatInterface = (MasterHeartBeatApiInterface) UnicastRemoteObject
				.exportObject(masterHBApi, 0);
		// Bind the remote object's stub in the registry
		registryHB.rebind("masterHeartBeatApi", masterHeartBeatInterface);
		System.out.println("master HeartBeat has started"); // 父进程死亡，子进程变为孤儿进程
		
		
		Registry registryUDS = 	LocateRegistry.getRegistry(Short.parseShort(masterProper.getProperty("rmi_masterDataStoreUserPort")));
		/**********UserDataStore  registry**********/
		UserDataStoreApi	 userDSApi = new UserDataStoreApi(
				dataStoreTaskChunkQueue,
				dataStoreTaskSlaverChunkMap,
				dataStoreTaskSlaverChunkQueue,
				 ftpFileDownedSizeMap
				);
		UserDataStoreApiInterface userDataStoreApiInterface = (UserDataStoreApiInterface) UnicastRemoteObject
				.exportObject(userDSApi, 0);
		// Bind the remote object's stub in the registry
		registryUDS.rebind("userDataStoreApi", userDataStoreApiInterface);
		System.out.println("user DataStore has started");
		
		
		Registry registrySDS = 	LocateRegistry.getRegistry(Short.parseShort(masterProper.getProperty("rmi_masterDataStoreSlaverPort")));
		/**********SlaverDataStore  registry**********/
		SlaverDataStoreApi	 slaverDSApi = new SlaverDataStoreApi(
				dataStoreTaskSlaverChunkQueue,
				dataStoreTaskSlaverChunkMap,
				 ftpFileDownedSizeMap
				);
		SlaverDataStoreApiInterface slaverDataStoreApiInterface = (SlaverDataStoreApiInterface) UnicastRemoteObject
				.exportObject(slaverDSApi, 0);
		// Bind the remote object's stub in the registry
		registrySDS.rebind("slaverDataStoreApi", slaverDataStoreApiInterface);
		System.out.println("slaver DataStore has started");
	}
}
